package com.example.sellersale.server.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sellersale.entity.CartInfo;
import com.example.sellersale.entity.Goods;
import com.example.sellersale.entity.Order;
import com.example.sellersale.mapper.OrderMapper;
import com.example.sellersale.server.CartService;
import com.example.sellersale.server.OrderService;
import com.example.sellersale.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

   @Autowired
   private OrderMapper orderMapper;
   @Autowired
   private CartService cartService;

    //返回所有用户的订单信息
    public List<Order> allUserOrder(){
        List<Order> orders = orderMapper.selectList();
        HashMap<String ,Order> orderList = new HashMap<>();
        for(Order o:orders){
            String id = o.getId();
            Double price = o.getPrice();
            Integer num = o.getOrderNum();
            Goods goods = o.getGoods();
            String delivery = o.getDelivery();
            System.out.println("============"+delivery);
            if(!orderList.containsKey(id)){
                o.setOrderNum(0);
                o.setPrice(0.0);
                o.setGoods(null);
                o.setGoodsId(null);
                //o.setGoodsId(null);
                orderList.put(id,o);
            }
            if(goods!=null){
                goods.setStatus(delivery);
                goods.setGoodsNum(num);
                goods.setOutPrice(price);
            }
            Order order = orderList.get(id);
            //先将上面的新的商品加入到订单模块
            order.getGoodsList().add(goods);
            //对订单进行设置(订单种商品数量的增加；商品价格的增加）
            order.addPrice(price);order.addNum(num);
            orderList.put(id,order);//因为对order进行修改过
        }
        System.out.println("================所有用户订单信息==================");
        System.out.println(orderList.values());
        ArrayList<Order> orders1 = new ArrayList<>(orderList.values());
        return orders1;
    }

    //查询用户个人订单信息
    public List<Order> selectOneOrder(String uId){
        List<Order> orders = orderMapper.selectOneOrder(uId);
        HashMap<String ,Order> orderList = new HashMap<>();
        for(Order o:orders){
            String id = o.getId();
            Double price = o.getPrice();
            Integer num = o.getOrderNum();
            Goods goods = o.getGoods();
            String delivery = o.getDelivery();
            System.out.println("============"+delivery);
            if(!orderList.containsKey(id)){
                o.setOrderNum(0);
                o.setPrice(0.0);
                o.setGoods(null);
                o.setGoodsId(null);
                orderList.put(id,o);
            }
            if(goods!= null){
                goods.setStatus(delivery);
                goods.setGoodsNum(num);
                goods.setOutPrice(price);
            }

            Order order = orderList.get(id);
            //先将上面的新的商品加入到订单模块
            order.getGoodsList().add(goods);
            //对订单进行设置(订单种商品数量的增加；商品价格的增加）
            order.addPrice(price);order.addNum(num);
            orderList.put(id,order);//因为对order进行修改过
        }
        System.out.println("================用户个人订单信息==================");
        System.out.println(orderList.values());
        ArrayList<Order> orders1 = new ArrayList<>(orderList.values());
        return orders1;
    }


    //修改订单状态
    public boolean updateOrderStatus(String oId,String gId,String status){
        int update = orderMapper.update(oId, gId, status);
        return update==1;
    }

    @Override
    public Boolean addOrders(List<CartInfo> cart) {
        //将购物车列表封装为订单信息

        Order order = new Order();
        System.out.println("&&&&&&"+order.getId());
        String id = null;
        Integer insert = 0;
        for(CartInfo cartInfo:cart){
            cartService.delCartInfo(cartInfo.getId());
            order.setUserId(cartInfo.getUserId());
            order.setGoodsId(cartInfo.getGoodsId());
            order.setOrderNum(cartInfo.getCount());
            order.setPrice(cartInfo.getPrice());
            insert += orderMapper.insert(order);
            id = order.getId();
        }
        System.out.println("=============id= "+id+"===========");
        return insert==cart.size();
    }

    @Override
    public List<Order> speciallyTimeOrder(String time) {
        List<Order> orders = orderMapper.speciallyTimeOrder(time + "-%");
        HashMap<String ,Order> orderList = new HashMap<>();
        for(Order o:orders){
            String id = o.getId();
            Double price = o.getPrice();
            Integer num = o.getOrderNum();
            Goods goods = o.getGoods();
            String delivery = o.getDelivery();
            System.out.println("============"+delivery);
            if(!orderList.containsKey(id)){
                o.setOrderNum(0);
                o.setPrice(0.0);
                o.setGoods(null);
                o.setGoodsId(null);
                //o.setGoodsId(null);
                orderList.put(id,o);
            }
            if(goods!= null){
                goods.setStatus(delivery);
                goods.setGoodsNum(num);
                goods.setOutPrice(price);
            }

            Order order = orderList.get(id);
            //先将上面的新的商品加入到订单模块
            order.getGoodsList().add(goods);
            //对订单进行设置(订单种商品数量的增加；商品价格的增加）
            order.addPrice(price);order.addNum(num);
            orderList.put(id,order);//因为对order进行修改过
        }
        System.out.println("================所有用户订单信息==================");
        System.out.println(orderList.values());
        ArrayList<Order> orders1 = new ArrayList<>(orderList.values());
        return orders1;
    }
}
